package com.sa07.config;

import java.io.InputStream;

import org.apache.log4j.Logger;
import org.jdom2.Document;
import org.jdom2.input.SAXBuilder;

import com.sa07.exception.ConfigInitFailedException;

/**
 * xml配置初始化
 * 
 * @author Long.shangbo
 *
 * 2014-2-15 上午12:16:00
 */
public abstract class XmlConfigInit implements IConfigInit{
	
	protected static final Logger logger = Logger.getLogger(XmlConfigInit.class);
	
	private SAXBuilder sax = null;

	public XmlConfigInit(){
		sax = new SAXBuilder();
	}
	
	@Override
	public void init() throws ConfigInitFailedException{
		try {
			Document doc = sax.build(getConfigInputStream());
			analyzeDocment(doc);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("Init xml config failed, caused by -> " + e);
			throw new ConfigInitFailedException(e);
		}
	}
	
	/**
	 * 获得配置文件
	 * @return
	 */
	protected abstract InputStream getConfigInputStream() throws Exception;
	
	/**
	 * 解析文档对象
	 * @param doc
	 * @return 
	 */
	protected abstract void analyzeDocment(Document doc);
}
